#include "../afwwin.h"

using namespace one_rag::detail;


Win32Window::Win32Window():_wnd(0)
{
    //ctor
}

Win32Window::~Win32Window()
{
  if(_wnd)
    DestroyWindow(_wnd);
    //dtor
}

HWND Win32Window::CreateMacBar(ATOM classId, int x, int y, int w, int h, LPCTSTR strTitle,HMENU hMenu)
{
  // 1. register the window class
  DWORD style = WS_POPUP|WS_DLGFRAME;//WS_THICKFRAME;//WS_OVERLAPPEDWINDOW|WS_VISIBLE;
  DWORD styleEx=0;

  // 2. Create the window
  _wnd = CreateWindowEx(styleEx,(LPCTSTR)atm,strTitle,style,x,y,w,h,0,hMenu,App::GetInstance(),this);
  ShowWindow(_wnd,SW_SHOWNORMAL);
  UpdateWindow(_wnd);
  return _wnd;
}

HWND Win32Window::CreateOverlapped(ATOM classId, int x, int y, int w, int h, LPCTSTR strTitle,HMENU hMenu)
{
  // 1. register the window class
  DWORD style = WS_OVERLAPPEDWINDOW;
  DWORD styleEx=0;

  // 2. Create the window
  _wnd = CreateWindowEx(styleEx,(LPCTSTR)atm,strTitle,style,x,y,w,h,0,hMenu,App::GetInstance(),this);
  ShowWindow(_wnd,SW_SHOWNORMAL);
  UpdateWindow(_wnd);
  return _wnd;
}

HWND Win32Window::CreateControl(ATOM classId, int x, int y, int w, int h, int id)
{
  // 1. register the window class
  DWORD style = WS_CHILD|WS_VISIBLE;
  DWORD styleEx=0;

  // 2. Create the window
  _wnd = CreateWindowEx(styleEx,(LPCTSTR)atm,strTitle,style,x,y,w,h,0,hMenu,App::GetInstance(),this);
  ShowWindow(_wnd,SW_SHOWNORMAL);
  UpdateWindow(_wnd);
  return _wnd;
}

//HWND Win32Window::Create(Window& parent,int x, int y, int w, int h, LPCSTR strTitle,ATOM atm,DWORD style,DWORD styleEx,UINT id)
HWND Win32Window::Create(Window& parent,ATOM atm,int x, int y, int w, int h,UINT id,LPCSTR strTitle,DWORD style,DWORD styleEx)
{
  int err = 0;
  style|=(WS_CHILD|WS_VISIBLE);
  if(w<0)
  {
    SIZE sz=parent.GetClientSize();
    w = sz.cx;
    h = sz.cy;
  }
  _wnd = CreateWindowEx(styleEx,(LPCTSTR)atm,strTitle,style,x,y,w,h,parent,(HMENU)id,App::GetInstance(),this);
  ShowWindow(_wnd,SW_SHOWNORMAL);
  UpdateWindow(_wnd);
  if(!_wnd)
  {
    err = GetLastError();
  }
  return _wnd;
}

ATOM Win32Window::RegisterClass(LPCSTR strClassName,HBRUSH brush)
{
  WNDCLASSEX wc = {0};
  wc.cbSize = sizeof(wc);
  wc.hInstance = App::GetInstance();
  wc.lpszClassName = strClassName;
  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  wc.hbrBackground = brush;
  wc.lpfnWndProc = WindowProc;

  ATOM atm = RegisterClassEx(&wc);
  if(!atm)
  {
    WNDCLASSEX wc2;
    atm = GetClassInfoEx(App::GetInstance(),strClassName,&wc2);
  }
  return atm;
}

SIZE Win32Window::GetClientSize()
{
  RECT rc;
   GetClientRect(_wnd,&rc);
   SIZE sz = {rc.right,rc.bottom};
   return sz;
}

UINT Win32Window::SetTimer(DWORD ms,UINT id)
{
  return ::SetTimer(_wnd,id,ms,0L);
}

LRESULT Win32Window::WindowProc(UINT uMsg,WPARAM wParam,LPARAM lParam)
{
  return DefWindowProc(_wnd,uMsg,wParam,lParam);
}

LRESULT Win32Window::WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
  Window* self=0;
  if(uMsg == WM_CREATE) {
    CREATESTRUCT* pcs = (CREATESTRUCT*)lParam;
    self = (Window*)pcs->lpCreateParams;
    self->_wnd = hwnd;
    SetWindowLongPtr(hwnd,GWLP_USERDATA,(LONG_PTR)self);
  }
  else
    self = (Window*)GetWindowLongPtr(hwnd,GWLP_USERDATA);

    LRESULT ret=0;
    if(self){
      ret = self->WindowProc(uMsg,wParam,lParam);
      if( uMsg == WM_DESTROY) {
        if( (self=(Window*)GetWindowLongPtr(hwnd,GWLP_USERDATA))){
          self->_wnd=0;
          SetWindowLong(hwnd,GWLP_USERDATA,0);
        }
      }
    }
    else
      ret = DefWindowProc(hwnd,uMsg,wParam,lParam);
    return ret;
}
